Title:Programming. Principles and Practice Using C++ (2nd Edition).epub
- Title:Programming: Principles and Practice Using C++ (2nd Edition)
- Author:Bjarne Stroustrup
- Publisher:Pearson Education
- Date:2014-06-02T00:00:00+00:00
- Tag:
- Score:
Preface
“Damn the torpedoes! Full speed ahead.”
—Admiral Farragut
Programming is the art of expressing solutions to problems so that a computer can execute those solutions. Much of the effort in programming is spent finding and refining solutions. Often, a problem is only fully understood through the process of programming a solution for it.
This book is for someone who has never programmed before but is willing to work hard to learn. It helps you understand the principles and acquire the practical skills of programming using the C++ programming language. My aim is for you to gain sufficient knowledge and experience to perform simple useful programming tasks using the best up-to-date techniques. How long will that take? As part of a first-year university course, you can work through this book in a semester (assuming that you have a workload of four courses of average difficulty). If you work by yourself, don’t expect to spend less time than that (maybe 15 hours a week for 14 weeks).
Three months may seem a long time, but there’s a lot to learn and you’ll be writing your first simple programs after about an hour. Also, all learning is gradual: each chapter introduces new useful concepts and illustrates them with examples inspired by real-world uses. Your ability to express ideas in code — getting a computer to do what you want it to do — gradually and steadily increases as you go along. I never say, “Learn a month’s worth of theory and then see if you can use it.”
Why would you want to program? Our civilization runs on software. Without understanding software you are reduced to believing in “magic” and will be locked out of many of the most interesting, profitable, and socially useful technical fields of work. When I talk about programming, I think of the whole spectrum of computer programs from personal computer applications with GUIs (graphical user interfaces), through engineering calculations and embedded systems control applications (such as digital cameras, cars, and cell phones), to text manipulation applications as found in many humanities and business applications. Like mathematics, programming — when done well — is a valuable intellectual exercise that sharpens our ability to think. However, thanks to feedback from the computer, programming is more concrete than most forms of math, and therefore accessible to more people. It is a way to reach out and change the world — ideally for the better. Finally, programming can be great fun.
Why C++? You can’t learn to program without a programming language, and C++ directly supports the key concepts and techniques used in real-world software. C++ is one of the most widely used programming languages, found in an unsurpassed range of application areas. You find C++ applications everywhere from the bottom of the oceans to the surface of Mars. C++ is precisely and comprehensively defined by a nonproprietary international standard. Quality and/or free implementations are available on every kind of computer. Most of the programming concepts that you will learn using C++ can be used directly in other languages, such as C, C#, Fortran, and Java. Finally, I simply like C++ as a language for writing elegant and efficient code.
This is not the easiest book on beginning programming; it is not meant to be. I just aim for it to be the easiest book from which you can learn the basics of real-world programming. That’s quite an ambitious goal because much modern software relies on techniques considered advanced just a few years ago.
My fundamental assumption is that you want to write programs for the use of others, and to do so responsibly, providing a decent level of system quality; that is, I assume that you want to achieve a level of professionalism. Consequently, I chose the topics for this book to cover what is needed to get started with real-world programming, not just what is easy to teach and learn. If you need a technique to get basic work done right, I describe it, demonstrate concepts and language facilities needed to support the technique, provide exercises for it, and expect you to work on those exercises. If you just want to understand toy programs, you can get along with far less than I present. On the other hand, I won’t waste your time with material of marginal practical importance. If an idea is explained here, it’s because you’ll almost certainly need it.
If your desire is to use the work of others without understanding how things are done and without adding significantly to the code yourself, this book is not for you. If so, please consider whether you would be better served by another book and another language. If that is approximately your view of programming, please
also consider from where you got that view and whether it in fact is adequate for your needs. People often underestimate the complexity of programming as well as its value. I would hate for you to acquire a dislike for programming because of a mismatch between what you need and the pa
About This eBook
Title Page
Copyright Page
Contents
Preface
A note to students
A note to teachers
ISO standard C++
Support
Acknowledgments
0. Notes to the Reader
0.1 The structure of this book
0.2 A philosophy of teaching and learning
0.3 Programming and computer science
0.4 Creativity and problem solving
0.5 Request for feedback
0.6 References
0.7 Biographies
Bjarne Stroustrup
Lawrence “Pete” Petersen
Postscript
1. Computers, People, and Programming
1.1 Introduction
1.2 Software
1.3 People
1.4 Computer science
1.5 Computers are everywhere
1.6 Ideals for programmers
Review
Terms
Exercises
Postscript
Part I: The Basics
2. Hello, World!
2.1 Programs
2.2 The classic first program
2.3 Compilation
2.4 Linking
2.5 Programming environments
Drill
Review
Terms
Exercises
Postscript
3. Objects, Types, and Values
3.1 Input
3.2 Variables
3.3 Input and type
3.4 Operations and operators
3.5 Assignment and initialization
3.6 Composite assignment operators
3.7 Names
3.8 Types and objects
3.9 Type safety
Drill
Review
Terms
Exercises
Postscript
4. Computation
4.1 Computation
4.2 Objectives and tools
4.3 Expressions
4.4 Statements
4.5 Functions
4.6 vector
4.7 Language features
Drill
Review
Terms
Exercises
Postscript
5. Errors
5.1 Introduction
5.2 Sources of errors
5.3 Compile-time errors
5.4 Link-time errors
5.5 Run-time errors
5.6 Exceptions
5.7 Logic errors
5.8 Estimation
5.9 Debugging
5.10 Pre- and post-conditions
5.11 Testing
Drill
Review
Terms
Exercises
Postscript
6. Writing a Program
6.1 A problem
6.2 Thinking about the problem
6.3 Back to the calculator!
6.4 Grammars
6.5 Turning a grammar into code
6.6 Trying the first version
6.7 Trying the second version
6.8 Token streams
6.9 Program structure
Drill
Review
Terms
Exercises
Postscript
7. Completing a Program
7.1 Introduction
7.2 Input and output
7.3 Error handling
7.4 Negative numbers
7.5 Remainder: %
7.6 Cleaning up the code
7.7 Recovering from errors
7.8 Variables
Drill
Review
Terms
Exercises
Postscript
8. Technicalities: Functions, etc.
8.1 Technicalities
8.2 Declarations and definitions
8.3 Header files
8.4 Scope
8.5 Function call and return
8.6 Order of evaluation
8.7 Namespaces
Drill
Review
Terms
Exercises
Postscript
9. Technicalities: Classes, etc.
9.1 User-defined types
9.2 Classes and members
9.3 Interface and implementation
9.4 Evolving a class
9.5 Enumerations
9.6 Operator overloading
9.7 Class interfaces
9.8 The Date class
Drill
Review
Terms
Exercises
Postscript
Part II: Input and Output
10. Input and Output Streams
10.1 Input and output
10.2 The I/O stream model
10.3 Files
10.4 Opening a file
10.5 Reading and writing a file
10.6 I/O error handling
10.7 Reading a single value
10.8 User-defined output operators
10.9 User-defined input operators
10.10 A standard input loop
10.11 Reading a structured file
Drill
Review
Terms
Exercises
Postscript
11. Customizing Input and Output
11.1 Regularity and irregularity
11.2 Output formatting
11.3 File opening and positioning
11.4 String streams
11.5 Line-oriented input
11.6 Character classification
11.7 Using nonstandard separators
11.8 And there is so much more
Drill
Review
Terms
Exercises
Postscript
12. A Display Model
12.1 Why graphics?
12.2 A display model
12.3 A first example
12.4 Using a GUI library
12.5 Coordinates
12.6 Shapes
12.7 Using Shape primitives
12.8 Getting this to run
Drill
Review
Terms
Exercises
Postscript
13. Graphics Classes
13.1 Overview of graphics classes
13.2 Point and Line
13.3 Lines
13.4 Color
13.5 Line_style
13.6 Open_polyline
13.7 Closed_polyline
13.8 Polygon
13.9 Rectangle
13.10 Managing unnamed objects
13.11 Text